package com.mou.task;

import com.mou.constant.TransactionConstant;
import com.mou.entity.Transaction;
import com.mou.mapper.TransactionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private TransactionMapper transactionMapper;
    //处理超时订单
    @Scheduled(cron = "0 * * * * ?  ")
    //@Scheduled(cron = "1/5 * * * * ? ")
    public void processTimeoutOrder() {
        log.info("定时处理超时订单:{}", LocalDateTime.now());

        List<Transaction> transactions = transactionMapper.getByStatusAndOrderTimeLT(TransactionConstant.PENDING_PAYMENT, LocalDateTime.now().plusMinutes(-30));
        if(transactions != null&& !transactions.isEmpty()){
            for(Transaction transaction : transactions){
                transaction.setStatus(TransactionConstant.CANCELLED);
                transactionMapper.updateStatus(transaction);
            }
        }
        log.info("处理超时订单完成:{}", LocalDateTime.now());
    }
}
